﻿using System;
using System.Collections;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Spring.Context.Support;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Core.Extension;
using System.Collections.Generic;
using System.Linq;
using Huirui.Cavan.Domain.MainBoundedContext.ReportModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.Setting;

namespace Huirui.Cavan.Application.MainBoundedContext.PurchaseModule
{
    #region PurchaseOrder

    public class PurchaseOrderAppService : BaseAppService<PurchaseOrder, string, PurchaseOrderSearch, IPurchaseOrderRepository>, IPurchaseOrderAppService
    {
        private IProjectAppService ProjectAppService { get; set; }
        private ISupplierAppService SupplierAppService { get; set; }
        private IWareHouseAppService WareHouseAppService { get; set; }
        private IStockAppService StockAppService { get; set; }
        private IEmployeeAppService EmployeeAppService { get; set; }

        public override BoolGenericResult<PurchaseOrder> AddOrUpdate(PurchaseOrder entity)
        {
            Init();
            BoolGenericResult<PurchaseOrder> result = new BoolGenericResult<PurchaseOrder>();

            this.ProjectAppService = this.SpringContext.GetObject(SystemModuleConst.PROJECTAPPSERVICE) as IProjectAppService;
            this.SupplierAppService = this.SpringContext.GetObject(SystemModuleConst.SUPPLIERAPPSERVICE) as ISupplierAppService;
            this.WareHouseAppService = this.SpringContext.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;
            this.StockAppService = this.SpringContext.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
            this.EmployeeAppService = this.SpringContext.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;

            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Purchaser purchaser = new Purchaser(employee);

            if (entity.Project.IsNotNull() && entity.Project.Id.IsNotEmpty())
            {
                entity.Project = this.ProjectAppService.GetById(entity.Project.Id);
            }
            if (entity.Supplier.IsNotNull() && entity.Supplier.Id.IsNotEmpty())
            {
                entity.Supplier = this.SupplierAppService.GetById(entity.Supplier.Id);
            }
            if (entity.PurchaseEmployee.IsNotNull() && entity.PurchaseEmployee.Id.IsNotEmpty())
            {
                entity.PurchaseEmployee = this.EmployeeAppService.GetById(entity.PurchaseEmployee.Id);
            }
            if (entity.WareHouse.IsNotNull() && entity.WareHouse.Id.IsNotEmpty())
            {
                entity.WareHouse = this.WareHouseAppService.GetById(entity.WareHouse.Id);
            }
            foreach (PurchaseOrderDetail pod in entity.PurchaseOrderDetails)
            {
                pod.WareHouse = entity.WareHouse;
                pod.Stock = this.StockAppService.GetById(pod.Stock.Id);
            }
            entity.CurrencyRate = decimal.Parse(entity.CurrencyRate.ToString("N" + HuiruiProject.UserSettings.CurrencyPrecision.ToString()));

            PurchaseOrder oldPurchaseOrder = null;
            if (entity.Id.IsNotEmpty())
            {
                oldPurchaseOrder = this.Repository.Get(entity.Id);
            }

            if (oldPurchaseOrder.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.PurchaseOrder);

                result = purchaser.OrderStock(entity.Id, entity.PurchaseDate, entity.DeliveryDate, entity.Currency, entity.CurrencyRate, entity.SupplierInvoiceNumber, entity.DeliveryAddress, entity.Remark,
                    employee, entity.Project, entity.Supplier, entity.WareHouse, entity.Contact, entity.ContactTelephone, entity.PurchaseOrderDetails);
                if (result.IsSuccess)
                {
                    return base.Add(result.Object);
                }
            }
            else
            {
                result = purchaser.UpdatePurchaseOrder(oldPurchaseOrder, entity.PurchaseDate, entity.DeliveryDate, entity.Currency, entity.CurrencyRate, entity.SupplierInvoiceNumber, entity.DeliveryAddress, entity.Remark,
                    employee, entity.Project, entity.Supplier, entity.PurchaseOrderDetails);
                if (result.IsSuccess)
                    return base.Update(result.Object);
            }
            return result;
        }

        public IList<PurchaseOrderReport> GetPurchaseOrderReport(DateTime? startDate, DateTime? endDate, string purchaseOrderCode, string supplierCode, IList<PurchaseIdentifier.StockInStatus> stockInStatus)
        {
            return this.Repository.GetPurchaseOrderReport(startDate, endDate, purchaseOrderCode, supplierCode, stockInStatus);
        }

        public IList<PurchaseOrderManageReport> GetPurchaseOrderManageReport(DateTime? startDate, DateTime? endDate, string supplierCode, string purchaseOrderCode,
            string supplierInvoiceNumber, string purchaseEmployeeCode, string wareHouseCode, string checkedStatues, string stockInStatues)
        {
            return this.Repository.GetPurchaseOrderManageReport(startDate, endDate, supplierCode, purchaseOrderCode, supplierInvoiceNumber, purchaseEmployeeCode, wareHouseCode,
                checkedStatues, stockInStatues);
        }

        #region IPurchaseOrderAppService 成员


        public BoolGenericResult<PurchaseOrder> UpdatePurchaseOrderStatus(PurchaseOrder purchaseOrder, PurchaseIdentifier.StockInStatus status)
        {
            this.SpringContext = ContextRegistry.GetContext();
            BoolGenericResult<PurchaseOrder> result = new BoolGenericResult<PurchaseOrder>();

            this.EmployeeAppService = this.SpringContext.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            PurchaseManager purchaseManager = new PurchaseManager(employee);

            result = purchaseManager.CloseOrReopenPurchaseOrder(purchaseOrder, status);

            if (result.IsSuccess)
            {
                this.Repository.Update(result.Object);
                if (status == PurchaseIdentifier.StockInStatus.Closed)
                {
                    result.Messages.Add("关闭成功");
                }
                else
                {
                    result.Messages.Add("打开成功");
                }
            }

            return result;
        }

        #endregion
    }
    #endregion
}